//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmQuantoHelper.h"
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ fTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxVolTS, Double equityFxCorrelation, Double exchRateATMlevel) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CYieldTermStructure^ _CrTS;
    CYieldTermStructure^ _CfTS;
    CBlackVolTermStructure^ _CfxVolTS;
    try
    {
#ifdef HANDLE
        _phFdmQuantoHelper = NULL;
#endif
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _rTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CrTS->GetShared ()); 
        _CfTS = safe_cast<CYieldTermStructure^> (fTS);
        _CfTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _fTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CfTS->GetShared ()); 
        _CfxVolTS = safe_cast<CBlackVolTermStructure^> (fxVolTS);
        _CfxVolTS->Lock();
        boost::shared_ptr<QuantLib::BlackVolTermStructure>& _fxVolTS = static_cast<boost::shared_ptr<QuantLib::BlackVolTermStructure>&> (_CfxVolTS->GetShared ()); 
        QuantLib::Real _equityFxCorrelation = (QuantLib::Real)ValueHelper::Convert (equityFxCorrelation); //d
        QuantLib::Real _exchRateATMlevel = (QuantLib::Real)ValueHelper::Convert (exchRateATMlevel); //d
        _ppFdmQuantoHelper = new boost::shared_ptr<QuantLib::FdmQuantoHelper> (new QuantLib::FdmQuantoHelper ( _rTS,  _fTS,  _fxVolTS,  _equityFxCorrelation,  _exchRateATMlevel ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CfTS != nullptr) _CfTS->Unlock();
        if (_CfxVolTS != nullptr) _CfxVolTS->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (boost::shared_ptr<QuantLib::FdmQuantoHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmQuantoHelper = NULL;
#endif
	_ppFdmQuantoHelper = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (QuantLib::FdmQuantoHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmQuantoHelper = NULL;
#endif
	_ppFdmQuantoHelper = new boost::shared_ptr<QuantLib::FdmQuantoHelper> (&childNative);
    
    _FdmQuantoHelperOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (CFdmQuantoHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmQuantoHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmQuantoHelper = new boost::shared_ptr<QuantLib::FdmQuantoHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmQuantoHelper = NULL;
#endif
	if (!t->IsSubclassOf(CFdmQuantoHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (QuantLib::Handle<QuantLib::FdmQuantoHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmQuantoHelper = &childNative;
	_ppFdmQuantoHelper = &static_cast<boost::shared_ptr<QuantLib::FdmQuantoHelper>>(childNative.currentLink());
    
    _FdmQuantoHelperOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (QuantLib::Handle<QuantLib::FdmQuantoHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmQuantoHelper = &childNative;
	_ppFdmQuantoHelper = &static_cast<boost::shared_ptr<QuantLib::FdmQuantoHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::CFdmQuantoHelper (QuantLib::FdmQuantoHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmQuantoHelper = NULL;
#endif
	_ppFdmQuantoHelper = new boost::shared_ptr<QuantLib::FdmQuantoHelper> (new QuantLib::FdmQuantoHelper (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::~CFdmQuantoHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmQuantoHelper != NULL)
    {
	    delete _ppFdmQuantoHelper;
        _ppFdmQuantoHelper = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::!CFdmQuantoHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmQuantoHelper != NULL)
    {
	    delete _ppFdmQuantoHelper;
    }
}
QuantLib::FdmQuantoHelper& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::GetReference ()
{
    if (_ppFdmQuantoHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmQuantoHelper;
}
boost::shared_ptr<QuantLib::FdmQuantoHelper>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::GetShared ()
{
    if (_ppFdmQuantoHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmQuantoHelper;
}
QuantLib::FdmQuantoHelper* Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::GetPointer ()
{
    if (_ppFdmQuantoHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmQuantoHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmQuantoHelper>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::GetHandle ()
{
	if (_phFdmQuantoHelper == NULL)
	{
		_phFdmQuantoHelper = new Handle<QuantLib::FdmQuantoHelper> (*_ppFdmQuantoHelper);
	}
	return *_phFdmQuantoHelper;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::HasNative () 
{
	return (_ppFdmQuantoHelper != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper::QuantoAdjustment (Double equityVol, Double t1, Double t2)
{
    try
    {
        QuantLib::Volatility _equityVol = (QuantLib::Volatility)ValueHelper::Convert (equityVol); //a
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppFdmQuantoHelper)->quantoAdjustment ( _equityVol,  _t1,  _t2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^ Cephei::QL::Methods::Finitedifferences::Utilities::CFdmQuantoHelper_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ fTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ fxVolTS, Double equityFxCorrelation, Double exchRateATMlevel)
{
    return REFNEW CFdmQuantoHelper ( rTS,  fTS,  fxVolTS,  equityFxCorrelation,  exchRateATMlevel);
}
